import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  Index,
} from 'typeorm';

export enum DeviceType {
  WECHAT = 'wechat',
  H5 = 'H5',
  APP = 'App',
}

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ unique: true, comment: '微信用户唯一标识' })
  @Index()
  openId: string;

  @Column({ nullable: true, comment: '微信开放平台统一ID' })
  @Index()
  unionid?: string;

  @Column({ default: '', comment: '用户昵称' })
  nickName?: string;

  @Column({ default: '', comment: '头像URL' })
  avatar?: string;

  @Column({ nullable: true, default: '', comment: '加密手机号' })
  @Index()
  mobile?: string;

  @Column({ default: false, comment: '是否管理员' })
  is_admin: boolean;

  @CreateDateColumn({
    type: 'timestamp',
    precision: 6,
    default: () => 'CURRENT_TIMESTAMP(6)',
    comment: '注册时间',
  })
  created_at: Date;

  @Column({
    type: 'timestamp',
    precision: 6,
    default: () => 'CURRENT_TIMESTAMP(6)',
    onUpdate: 'CURRENT_TIMESTAMP(6)',
    comment: '最后登录时间',
  })
  last_login: Date;

  @Column({
    type: 'enum',
    enum: DeviceType,
    default: DeviceType.WECHAT,
    comment: '登录设备类型',
  })
  device_info: DeviceType;

  @Column({ comment: '加密会话密钥' })
  sessionKey: string;

  @Column({ default: 'account', comment: '登录方式：account/wechat' })
  loginType?: string;
}
